#include "glb_res.h"

QueueHandle_t OTA_Update_Cmd_Queue = NULL;
SemaphoreHandle_t Log_Mutex = NULL;

void Each_Semaphore_Init() {

    Log_Mutex = xSemaphoreCreateMutex();
    if (!Log_Mutex) {
        printf("Log_Mutex Create Failed\r\n");
    }

}

void Each_xBinarySemaphore_Init() {

}

void Msg_Queue_Init() {

    OTA_Update_Cmd_Queue = xQueueCreate(3, sizeof(OTA_Update_Cmd_Typedef));
    if (OTA_Update_Cmd_Queue == NULL) {
        CH32_LOGI("Msg_Queue_Init", "failed to creat OTA_Update_Cmd_Queue");
    }
}


void vPeriodicTimerCallback(TimerHandle_t xTimer) {

}

void Soft_Timer_Init() {

}

void Global_resources_Init() {
    Each_Semaphore_Init();
    Each_xBinarySemaphore_Init();
    Msg_Queue_Init();
    Soft_Timer_Init();
}


/**
 * @brief 多线程下安全的日志打印
 * @param format字符串， 可变参数。如printf()的参数
 * @return none
 */
void LogOutput(const char* format, ...) {

    va_list args;
    va_start(args, format);

    if(xSemaphoreTake(Log_Mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
        vprintf(format, args);
        xSemaphoreGive(Log_Mutex);
    }

    va_end(args);
}
